﻿using System;
using Bricks.Injecting;
using Microsoft.Practices.Unity;

namespace Bricks.Unity.Injecting
{

    /// <summary>
    /// 
    /// </summary>
    public class UnityContainerRegistryFacade : ContainerRegistryFacadeBase<IUnityContainer>, IUnityContainerRegistryFacade
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="unityContainer"></param>
        public UnityContainerRegistryFacade(IUnityContainer unityContainer) :
            base(unityContainer)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        /// <param name="key"></param>
        /// <param name="service"></param>
        /// <returns></returns>
        public override IContainerRegistryFacade Register(Type serviceType, string key, object service)
        {
            Execute(unityContainer => unityContainer.RegisterInstance(serviceType, key, service));

            return this;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <param name="key"></param>
        /// <param name="service"></param>
        /// <returns></returns>
        public override IContainerRegistryFacade Register<TService>(string key, TService service)
        {
            Execute(unityContainer => unityContainer.RegisterInstance<TService>(key, service));

            return this;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public override IContainerRegistryFacade Register(Type serviceType, string key)
        {
            Execute(unityContainer => unityContainer.RegisterType(serviceType, key));

            return this;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public override IContainerRegistryFacade Register<TService>(string key)
        {
            Execute(unityContainer => unityContainer.RegisterType<TService>(key));

            return this;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        /// <param name="implType"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public override IContainerRegistryFacade Register(Type serviceType, Type implType, string key)
        {
            Execute(unityContainer => unityContainer.RegisterType(serviceType, implType, key));

            return this;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <typeparam name="TImpl"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public override IContainerRegistryFacade Register<TService, TImpl>(string key)
        {
            Execute(unityContainer => unityContainer.RegisterType<TService, TImpl>(key));

            return this;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public override bool IsRegistered(Type serviceType, string key)
        {
            return Execute<bool>(unityContainer => unityContainer.IsRegistered(serviceType, key));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public override bool IsRegistered<TService>(string key)
        {
            return Execute<bool>(unityContainer => unityContainer.IsRegistered<TService>(key));
        }
    }
}
